package com.woniu.backstage.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.backstage.common.PageResult;
import com.woniu.backstage.common.Result;
import com.woniu.backstage.entity.TbBanned;
import com.woniu.backstage.entity.TbUser;
import com.woniu.backstage.entity.form.UserForm;
import com.woniu.backstage.entity.form.VipsForm;
import com.woniu.backstage.service.ITbBannedService;
import com.woniu.backstage.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author spirit
 * @since 2020-04-15
 */
@RestController
@RequestMapping("/backstage")
public class TbUserController extends BaseController {
    @Autowired
    private ITbUserService userService;
    @Autowired
    private ITbBannedService bannedService;

    @RequestMapping("/userShow")
    public String getUser(int page,int limit){
        int pageSize = limit;
        PageResult result = userService.getAllUser(page, pageSize);
        return JSON.toJSONString(result);
    }

    @RequestMapping("/addUser")
    public Result addUser(TbUser user) throws Exception {
        Date createTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(createTime);
        System.out.println(time);
        user.setCreateTime(time);
        user.setLevel(0);
        int result = userService.addUser(user);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("添加失败");
    }

    @RequestMapping("/deleteUser")
    public Result deleteUser(Integer id) throws Exception {
        int result = userService.deleteUser(id);
        System.out.println(result);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

    @RequestMapping("/updateVips")
    public Result updateVips(TbUser user) throws Exception {
        Long id = user.getId();
        TbUser userInfo = userService.selectById(id);
        int res = userInfo.getVipStartTime().compareTo(user.getVipEndTime());
        if(res > 0){
            return Result.fail();
        }
        int result = userService.updateUser(user);
        System.out.println(result);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("修改失败");
    }

    @RequestMapping("/updateUser")
    public Result updateUser(TbUser user) throws Exception {
        System.out.println("user:"+user);
        if(user.getLevel() == 4){
            TbBanned ban = bannedService.getBanned(user.getId());
            if(ban != null){
                return Result.fail();
            }else{
                TbBanned banned = new TbBanned();
                banned.setUserId(user.getId());
                Date bannedTime = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(bannedTime);
                System.out.println("封禁开始时间："+time);
                long times = bannedTime.getTime();
                long addTimes = 7*24*60*60*1000;
                times+=addTimes;
                Date date = new Date(times);
                String stopTime = simpleDateFormat.format(date);
                System.out.println("封禁结束时间："+stopTime);
                banned.setBannedTime(time);
                banned.setStopTime(stopTime);
                bannedService.addBanned(banned);
                int result = userService.updateUser(user);
                System.out.println(result);
                if(result == 1){
                    return Result.success();
                }
                throw new Exception("修改失败");
            }
        }else{
            Integer integer = bannedService.deleteBanned(user.getId());
            System.out.println("解禁："+integer);
            int result = userService.updateUser(user);
            if(result == 1){
                return Result.success();
            }
            throw new Exception("修改失败");
        }
    }

    @RequestMapping("/deleteBatch")
    public Result deleteBatch(Integer[] idArr) throws Exception {
        Integer result = userService.deleteBatch(Arrays.asList(idArr));
        System.out.println(result);
        if(result > 0){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

    @RequestMapping("/pageLimit")
    public Result pageLimit(Integer page, Integer limit, UserForm userForm){
        System.out.println("userForm:"+userForm);
        Page<TbUser> userPage = new Page<>(page, limit);
        IPage<TbUser> userIPage = userService.pageLimit(userPage,userForm);
        //获取查询的数据
        List<TbUser> users = userIPage.getRecords();
        System.out.println("users:"+users);
        //获取总的数据条数
        long total = userIPage.getTotal();
        return Result.success(users, (int) total);
    }

    @RequestMapping("/vipsShow")
    public Result vipsShow(Integer page, Integer limit, VipsForm vipsForm){
        System.out.println("vipsForm:"+vipsForm);
        Page<TbUser> vipsPage = new Page<>(page, limit);
        IPage<TbUser> vipsIPage = userService.vipsShow(vipsPage,vipsForm);
        //获取查询的数据
        List<TbUser> vips = vipsIPage.getRecords();
        System.out.println("vips:"+vips);
        //获取总的数据条数
        long total = vipsIPage.getTotal();
        return Result.success(vips, (int) total);
    }

}

